<?php

class UrbacModule extends CWebModule {
    public $usersTable = 'Users';

    public $superUser = 'Authority';

    public $hashWord = 'hash';

    public $defaultRole = 'User';

    public $enableRegistration = false;

    public $emailConfirmation = true;

    public $emailFrom = 'no-reply@downloadNow.pl';




    public function init() {
    // this method is called when the module is being created
    // you may place code here to customize the module or the application

    // import the module-level models and components
        $this->setImport(array(
            'urbac.models.*',
            'urbac.components.*',
        ));

        // publish css
        $cssDir  = Yii::getPathOfAlias("urbac.css");
        $cssFile = Yii::app()->assetManager->publish($cssDir).'/urbac.css';
        Yii::app()->clientScript->registerCssFile($cssFile);
    }

    public function beforeControllerAction($controller, $action) {
        if(parent::beforeControllerAction($controller, $action)) {
        // this method is called before any module controller action is performed

            if($this->isInstalled()){
//                FB::log('installed');
                return true;
            }
            else {
//                FB::log('not installed');
                $this->install();
                return true;
            }

        }
        else
            return false;
    }

    public function isInstalled() {

        if($module = Yii::app()->getModule('urbac')) {

            try {
                $tables = Yii::app()->db->schema->tableNames;
                $tableName = $module->usersTable;
                if(in_array($tableName, $tables)) {
                    return true;
                }

                return false;
            }
            catch (CDbException  $ex ) {
                return false;
            }
        }

    }

    public function install() {
        UrbacHelper::createTable();
    }
}
